package com.lant.config;

import java.util.EnumSet;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.DispatcherType;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.lant.web.interceptor.LoginHandlerInterceptor;

/**
 * @description: web工程配置
 * @projectName: lant-manager-web
 * @className: WebConfig
 * @author: YFL
 * @createTime: 2020年4月16日 下午6:47:22
 */
@Configuration
@EnableEurekaClient
@EnableFeignClients
@PropertySource({ "classpath:resource.properties" })
public class WebConfig implements WebMvcConfigurer {
	@Bean
	@LoadBalanced
	RestTemplate restTemplate() {
		return new RestTemplate();
	}

	/**
	 * @Title: addInterceptors
	 * @Description: 在springboot2.0.0之后继承WebMvcConfigurationSupport类，重写addInterceptors方法
	 * @param: registry
	 * @author: YFL
	 * @date: 2020年5月21日 下午7:29:51
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		LoginHandlerInterceptor loginHandlerInterceptor = new LoginHandlerInterceptor();
		loginHandlerInterceptor.setInherentEscapeURIS(this.authorizeUrl());
		/**
		 * addInterceptor 拦截器按照顺序执行,如果不同拦截器拦截存在相同的URL，前面的拦截器会执行，后面的拦截器将不执行
		 */
		registry.addInterceptor(loginHandlerInterceptor).addPathPatterns("/**").excludePathPatterns("/webjars/**",
				"/layui/**", "/iview/**", "/js/**", "/css/**", "/img/**");
	}

	/**
	 * @Title: authorizeUrl
	 * @Description: 不需要验证登录页面
	 * @return: Set<String>
	 * @author: YFL
	 * @date: 2020年5月21日 下午7:30:08
	 */
	private Set<String> authorizeUrl() {
		Set<String> excludeURIs = new HashSet<>();
		excludeURIs.add("/lant/getVerify"); // 登录验证码
		excludeURIs.add("/lant/checkVerify"); // 校验验证码
		excludeURIs.add("/lant/getSMSVerify"); // 获取短信验证码
		excludeURIs.add("/lant/checkSMSVerify"); // 校验短信验证码
		excludeURIs.add("/lant/user/loginPage"); // 登录页，需要拦截，直接token登录
		excludeURIs.add("/lant/user/login"); // 用户登录
		excludeURIs.add("/lant/user/logout"); // 退出登录,清空session
		excludeURIs.add("/lant/user/register"); // 用户注册页面
		excludeURIs.add("/lant/user/save"); // 用户注册
		return excludeURIs;
	}

	/**
	 * encodingFilter 编码的统一处理
	 */
	@Bean
	public FilterRegistrationBean<CharacterEncodingFilter> encodingFilter() {
		FilterRegistrationBean<CharacterEncodingFilter> filterBean = new FilterRegistrationBean<>();
		CharacterEncodingFilter filter = new CharacterEncodingFilter();
		filter.setEncoding("UTF-8");
		filter.setForceEncoding(true);

		filterBean.setFilter(filter);
		EnumSet<DispatcherType> dispatcherTypes = EnumSet.allOf(DispatcherType.class);
		dispatcherTypes.add(DispatcherType.REQUEST);
		dispatcherTypes.add(DispatcherType.FORWARD);
		filterBean.setDispatcherTypes(dispatcherTypes);
		filterBean.setEnabled(true);
		filterBean.addUrlPatterns("/*");
		return filterBean;
	}
}
